package com.front.managementfront.dictionary.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.front.managementfront.dictionary.entity.HktsDictionaryItem;
import com.front.managementfront.dictionary.entity.HktsDictionaryList;
import com.front.managementfront.dictionary.mapper.HktsDictionaryItemMapper;
import com.front.managementfront.dictionary.mapper.HktsDictionaryListMapper;
import com.front.managementfront.dictionary.service.IHktsDictionaryItemService;
import com.front.managementfront.dictionary.service.IHktsDictionaryListService;
import com.front.managementfront.dictionary.vo.DictionaryVo;
import com.front.managementfront.fegin.IUserAppServiceClient;
import com.hkts.management.common.utils.SnowFlakeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-05-09
 */
@Service
@Transactional
public class HktsDictionaryListServiceImpl extends ServiceImpl<HktsDictionaryListMapper, HktsDictionaryList> implements IHktsDictionaryListService {


    @Autowired
    private HktsDictionaryListMapper hktsDictionaryListMapper;

    @Autowired
    private HktsDictionaryItemMapper hktsDictionaryItemMapper;
    @Autowired
    public IHktsDictionaryItemService iHktsDictionaryItemService;
    @Autowired
    private IUserAppServiceClient iUserAppServiceClient;

    @Override
    public IPage<HktsDictionaryList> selectPage(Page<HktsDictionaryList> page, HktsDictionaryList hktsDictionaryList) {
        QueryWrapper<HktsDictionaryList> query = Wrappers.query();
        QueryWrapper<HktsDictionaryList> entityWrapper = getEntityWrapper(query, hktsDictionaryList);
        return hktsDictionaryListMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<HktsDictionaryList> selectList(HktsDictionaryList hktsDictionaryList) {
        QueryWrapper<HktsDictionaryList> query = Wrappers.query();
        QueryWrapper<HktsDictionaryList> entityWrapper = getEntityWrapper(query, hktsDictionaryList);
        return hktsDictionaryListMapper.selectList(entityWrapper);
    }

    /**
     * 根据key封装数据
     */
    @Override
    public DictionaryVo hktsDictionaryListGetByKey(String key) {
        HktsDictionaryList hktsDictionaryList = new HktsDictionaryList();
        QueryWrapper<HktsDictionaryList> query = Wrappers.query();
        hktsDictionaryList.setDictionaryKey(key);
        QueryWrapper<HktsDictionaryList> entityWrapper = getEntityWrapper(query, hktsDictionaryList);
        HktsDictionaryList hktsDictionaryList1 = hktsDictionaryListMapper.selectOne(entityWrapper);
        if (hktsDictionaryList1 == null) {
            return null;
        }
        HktsDictionaryItem item = new HktsDictionaryItem();
        item.setListId(hktsDictionaryList1.getId());
        item.setIsDisable("0");
        List<HktsDictionaryItem> hktsDictionaryItems = iHktsDictionaryItemService.selectList(item);
        DictionaryVo vo = new DictionaryVo(hktsDictionaryList1, hktsDictionaryItems);
        return vo;
    }
    /*
     * 字典新增和修改
     * */

    @Override
    public boolean saveOrUpdate(HktsDictionaryList entity) {
        /*
         * 校验key 或 listName 是否重复
         * */
        QueryWrapper<HktsDictionaryList> query = Wrappers.query();
        query.ne(StringUtils.isNotBlank(entity.getId()), HktsDictionaryList.ID, entity.getId());
        query.and(hktsDictionaryListQueryWrapper -> hktsDictionaryListQueryWrapper.eq(HktsDictionaryList.DICTIONARY_KEY, entity.getDictionaryKey()).or().eq(HktsDictionaryList.LIST_NAME, entity.getListName()));
        List<HktsDictionaryList> hktsDictionaryLists = hktsDictionaryListMapper.selectList(query);
        if (hktsDictionaryLists.size() > 0) {
            throw new RuntimeException("字典key或名称重复！");
        }
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()));
            entity.setCreateTime(LocalDateTime.now());
            entity.setCreateBy(iUserAppServiceClient.remoteUserBasicInfo().getResult().getNickName());
        }else{
            entity.setUpdateBy(iUserAppServiceClient.remoteUserBasicInfo().getResult().getNickName());
            entity.setUpdateTime(LocalDateTime.now());
        }
        if (entity.getDictionaryOrder()==null){
            Integer maxOrder=hktsDictionaryListMapper.selectMaxOrder();
            entity.setDictionaryOrder(maxOrder==null ? 1 : maxOrder+1);
        }
        if(entity.getDelFlag()==null){
            entity.setDelFlag(0);
        }
        return super.saveOrUpdate(entity);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<HktsDictionaryList> getEntityWrapper(QueryWrapper<HktsDictionaryList> entityWrapper, HktsDictionaryList hktsDictionaryList) {
        //条件拼接
        if (StringUtils.isNotBlank(hktsDictionaryList.getCreateBy())) {
            entityWrapper.like(HktsDictionaryList.CREATE_BY, hktsDictionaryList.getCreateBy());
        }
               /* if(StringUtils.isNotBlank(hktsDictionaryList.getCreateTime())){
                entityWrapper.like(HktsDictionaryList.CREATE_TIME,hktsDictionaryList.getCreateTime());
                }
                if(StringUtils.isNotBlank(hktsDictionaryList.getDelFlag())){
                entityWrapper.like(HktsDictionaryList.DEL_FLAG,hktsDictionaryList.getDelFlag());
                }*/
        if (StringUtils.isNotBlank(hktsDictionaryList.getUpdateBy())) {
            entityWrapper.like(HktsDictionaryList.UPDATE_BY, hktsDictionaryList.getUpdateBy());
        }
              /*  if(StringUtils.isNotBlank(hktsDictionaryList.getUpdateTime())){
                entityWrapper.like(HktsDictionaryList.UPDATE_TIME,hktsDictionaryList.getUpdateTime());
                }*/
        if (StringUtils.isNotBlank(hktsDictionaryList.getDictionaryKey())) {
            entityWrapper.like(HktsDictionaryList.DICTIONARY_KEY, hktsDictionaryList.getDictionaryKey());
        }
               /* if(StringUtils.isNotBlank(hktsDictionaryList.getDictionaryOrder())){
                entityWrapper.like(HktsDictionaryList.DICTIONARY_ORDER,hktsDictionaryList.getDictionaryOrder());
                }*/
        if (StringUtils.isNotBlank(hktsDictionaryList.getIsDisable())) {
            entityWrapper.like(HktsDictionaryList.IS_DISABLE, hktsDictionaryList.getIsDisable());
        }
        if (StringUtils.isNotBlank(hktsDictionaryList.getIsMultiple())) {
            entityWrapper.like(HktsDictionaryList.IS_MULTIPLE, hktsDictionaryList.getIsMultiple());
        }
        if (StringUtils.isNotBlank(hktsDictionaryList.getListName())) {
            entityWrapper.like(HktsDictionaryList.LIST_NAME, hktsDictionaryList.getListName());
        }
        return entityWrapper;
    }
}
